package com.xudu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xudu.entity.Student;
import com.xudu.mapper.StudentMapper;
import com.xudu.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xudu
 * @since 2024-04-21
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public boolean add(Student student) {
        int i = studentMapper.insert(student);
        if (i>0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public boolean del(Student student) {
        int i = studentMapper.deleteById(student.getId());
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean batchdel(List<Long> list) {

        int i = studentMapper.deleteBatchIds(list);
        if (i==list.size()){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public boolean update(Student student) {
        int i = studentMapper.updateById(student);
        if (i>0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public List<Student> getAll() {
        List<Student> students = studentMapper.selectList(null);

        return students;
    }

    @Override
    public Student getByNum(Student student) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Student::getId,student.getId());
        Student selectOne = studentMapper.selectOne(queryWrapper);
        return selectOne;
    }

    @Override
    public List<Student> fuzzySearchByName(String string) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(Student::getName,string);
        List<Student> students = studentMapper.selectList(queryWrapper);
        return students;
    }

    @Override
    public PageInfo<Student> page(Integer current, Integer size) {
        //获取第1页，10条内容，默认查询总数count
        PageHelper.startPage(current, size);
        List<Student> list = this.list();
        //用PageInfo对结果进行包装
        PageInfo page = new PageInfo(list);
        return page;
    }
}
